home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-06-10 | 7.4 KB | 250 lines | [TEXT/MPS ] |
- #ifndef __MACAPP__
- #include <MacApp.h>
- #endif
-
- #ifndef __FONTS__
- #include <Fonts.h>
- #endif
-
- #ifndef __PROTOVIEWS__
- #include "ProtoViews.h"
- #endif
-
- //------------------------------ •TLineData• ---------------------------------
- // Basic data element class used by the "TLineServer" to deliver data to the view
-
- //----------------------------------------------------------------------------------------
- // TLineData::ILineData:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
- pascal void TLineData::ILineData(const long offset,
- TFile *aFile)
- {
- fOffset = offset;
- fMyFile = aFile;
- }
-
- //----------------------------------------------------------------------------------------
- // TLineData::GetText:
- //----------------------------------------------------------------------------------------
- #pragma segment GVRes
- pascal void TLineData::GetText(CStr255 &theText)
- {
- char theBuffer[256];
- long count = 255;
- char cr = 13;
-
- fMyFile->SetDataMark(fOffset, fsFromStart);
- fMyFile->ReadUntil(theBuffer, count, cr);
- theBuffer[count] = 0;
- theText = theBuffer;
- }
-
-
- //------------------------------ •TLineServer• ---------------------------------
- // TLineServer reads a CR delimited text file and creates an instance of TLineData for
- // each line it finds.
-
- //----------------------------------------------------------------------------------------
- // TLineServer::ILineServer:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
- pascal void TLineServer::ILineServer(CStr255 &fileName)
- {
- ProcessSerialNumber psn;
- FSSpec fileSpec;
- char theBuffer[256];
- long count;
- char cr = 13;
-
- fLineList = NewList();
-
- // NOTE: assumption is that the text file lives in the same folder as the application.
- // Also note conspicuous lack of error checking
- // Hey, gimme a break, this was written for prototype
-
- OSErr theErr = FindProcessBySignature(gApplication->fCreator, psn, &fileSpec);
-
- // Note text file creator - If you're reading this you must have MPW
-
- fMyFile = NewFile('TEXT', 'MPS ', kUsesDataFork, kUsesRsrcFork, true, false);
- fMyFile->SpecifyWithTrio(fileSpec.vRefNum, fileSpec.parID, fileName);
-
- fMyFile->OpenFile();
-
- OSErr err = noErr;
- long offset = 0;
-
- do {
- count = 256;
- err = fMyFile->ReadUntil(theBuffer, count, cr);
- if(err == noErr) {
- TLineData *aLine = new TLineData;
- aLine->ILineData(offset, fMyFile);
- fLineList->InsertLast(aLine);
- fMyFile->GetDataMark(offset);
- }
- } while(err == noErr);
- }
-
- //----------------------------------------------------------------------------------------
- // TLineServer::GetALine:
- //----------------------------------------------------------------------------------------
- #pragma segment GVRes
- pascal void TLineServer::GetALine(const ArrayIndex theLine,
- CStr255 &theText)
- {
- TLineData *aLine = (TLineData *)fLineList->At(theLine);
- aLine->GetText(theText);
- }
-
- //----------------------------------------------------------------------------------------
- // TLineServer::GetLineCount:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
- pascal ArrayIndex TLineServer::GetLineCount()
- {
- return fLineList->GetSize();
- }
-
- //----------------------------------------------------------------------------------------
- // TLineServer::Free:
- //----------------------------------------------------------------------------------------
- #pragma segment MAClose
- pascal void TLineServer::Free()
- {
- fLineList->FreeList();
- fMyFile->Free();
- inherited::Free();
- }
-
-
- //------------------------------ •TLineView• ---------------------------------
- // Basic text display class.
- //----------------------------------------------------------------------------------------
- // TLineView::ILineView:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
- pascal void TLineView::ILineView(TLineServer *aLineServer)
- {
- VRect myExtent;
- FontInfo fi;
- VPoint limit;
-
- fMyLineServer = aLineServer;
- ArrayIndex lineCount = fMyLineServer->GetLineCount();
-
- this->GetExtent(myExtent); // Get our current size
- this->SetupDrawingEnvironment();
-
- TextFont(times);
- TextSize(12);
- TextFace(normal);
-
- GetFontInfo(fi);
- short lh = fi.ascent+fi.descent+fi.leading;
-
- limit.v = lh;
- limit.h = 0;
- ((TScroller *)fSuperView)->SetScrollParameters(limit,true,true);
-
- myExtent.bottom = myExtent.top + (lh*lineCount);
- this->SetFrame(myExtent,true);
- }
-
- //----------------------------------------------------------------------------------------
- // TLineView::Draw:
- //----------------------------------------------------------------------------------------
- #pragma segment GVRes
- pascal void TLineView::Draw(const VRect& area)
- {
- FontInfo fi;
-
- TextFont(times);
- TextSize(12);
- TextFace(normal);
- GetFontInfo(fi);
- short lh = fi.ascent+fi.descent+fi.leading;
-
- ArrayIndex firstLine = area.top / lh + 1;
- ArrayIndex lastLine = area.bottom / lh;
-
-
- if(lastLine > fMyLineServer->GetLineCount())
- lastLine = fMyLineServer->GetLineCount();
-
- CStr255 aLine;
- CRect qdRect;
- ViewToQDRect(area,qdRect);
- short where = qdRect.top + fi.ascent;
-
- for(ArrayIndex i=firstLine;i<=lastLine;i++) {
- fMyLineServer->GetALine(i, aLine);
- MoveTo(0,where);
- DrawString(aLine);
- where+=lh;
- }
- }
-
- //------------------------------ •TListView• ---------------------------------
- // Basic TextListView class
- //----------------------------------------------------------------------------------------
- // TListView::IListView:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
- pascal void TListView::IListView(TLineServer *aLineServer)
- {
- fMyLineServer = aLineServer;
- ArrayIndex lineCount = fMyLineServer->GetLineCount();
-
- if(fNumOfRows) // clear the view to zippo
- DelRowFirst(fNumOfRows);
-
- InsRowFirst((short)lineCount, 15); // match the supply of data
- }
-
- //----------------------------------------------------------------------------------------
- // TListView::GetItemText:
- //----------------------------------------------------------------------------------------
- #pragma segment GVRes
- pascal void TListView::GetItemText(short item,
- CStr255& aString)
- {
- fMyLineServer->GetALine(item, aString);
- }
-
-
- //------------------------------ •TMatrixView• ---------------------------------
- // basic TextGridViewClass
- //----------------------------------------------------------------------------------------
- // TMatrixView::IMatrixView:
- //----------------------------------------------------------------------------------------
- #pragma segment MAOpen
- pascal void TMatrixView::IMatrixView(TLineServer *aLineServer)
- {
- fMyLineServer = aLineServer;
- ArrayIndex lineCount = fMyLineServer->GetLineCount();
-
- short lines = (short)(lineCount/fNumOfCols);
- if(lines > 1)
- lines--;
- InsRowFirst(lines, 15); // match the supply of data
- }
-
- //----------------------------------------------------------------------------------------
- // TMatrixView::GetText:
- //----------------------------------------------------------------------------------------
- #pragma segment GVRes
- pascal void TMatrixView::GetText(GridCell item,
- CStr255& aString)
- {
- // This assumes that the data in the line server is arranged in row order, i.e.:
- // row1,col1
- // row1,col2
- // row2,col1
- // row2,col2 etc.
-
- ArrayIndex theOne = ((item.v - 1) * fNumOfCols) + item.h;
- fMyLineServer->GetALine(theOne, aString);
- }
-